---
- name: pull base image
  command: docker pull {{ atomic_reactor_git.base_registry }}/{{ atomic_reactor_git.base_image }}
  when: atomic_reactor_git.base_image is defined

- name: tag base image
  command: docker tag -f {{ atomic_reactor_git.base_registry }}/{{ atomic_reactor_git.base_image }} {{ atomic_reactor_git.base_image_retag }}
  when: atomic_reactor_git.base_image_retag is defined

- name: pull git repository with Dockerfile
  git:
    repo: "{{ atomic_reactor_git.git_url }}"
    dest: "{{ atomic_reactor_git.git_local_path }}"
    version: "{{ atomic_reactor_git.git_branch }}"
    accept_hostkey: yes
  register: dockerfile_git_repo
  changed_when: "dockerfile_git_repo.after|default('after') != dockerfile_git_repo.before|default('before')"

- name: check if atomic-reactor image is present
  command: docker inspect {{ atomic_reactor_tag }}
  register: image_present
  failed_when: image_present.rc != 0 and ('No such image' not in image_present.stderr)
  changed_when: image_present.rc != 0

- name: build atomic-reactor image
  command: docker build --no-cache=true --tag={{ atomic_reactor_tag }} {{ atomic_reactor_git.git_local_path }}/{{ atomic_reactor_git.git_subdir }}
  when: dockerfile_git_repo.changed or image_present.changed
